package com.timoon.manager.panel.product;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.tree.Tree;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;
import com.timoon.component.panel.bootstrap.modal.ModalPanel;
import com.timoon.component.panel.jlayout.JLayouterPanel;
import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.sample.dao.model.ProductSort;

/**
 *<p>功能:</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class ProductSortPanel extends BaseAuthPanel {
	
	@SpringBean(name="manager.product")
	private ProductSortService service;
	
	private ProductSort productSort=new ProductSort();
	
	private Integer sortTreeSelectedId;//选中的分类
	
	private JQueryFeedbackPanel feedbackPanel;
	
	private WebMarkupContainer treeContainer;
	
	private TextField sortNameField;
	
	private ModalPanel warnPanel;

	public ProductSortPanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderForm();
		
		renderFeedbackPanel();
		
		renderWarnPanel();
		
	}


	private void renderWarnPanel() {
		// TODO Auto-generated method stub
		this.warnPanel=new ModalPanel("wk_del_warn","警告","删除","确定删除所选分类及所有下级分类吗?") {
			@Override
			public boolean onAction(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				
				service.delProductSortById(sortTreeSelectedId);
				updateMyself(target);
				return true;
			}
		};
		add(warnPanel);
	}

	private void renderFeedbackPanel() {
		// TODO Auto-generated method stub
		this.feedbackPanel=new JQueryFeedbackPanel("wk_feedback");
		add(this.feedbackPanel);
	}

	private void renderForm() {
		// TODO Auto-generated method stub
		
		Form form=new Form<>("wk_form");
		add(form);
		
		renderDelBtn(form);
		
		renderTreeContainer(form);
		
		renderSortName(form);
	}

	private void renderTreeContainer(Form form) {
		// TODO Auto-generated method stub
		this.treeContainer=new WebMarkupContainer("wk_tree_container");
		treeContainer.setOutputMarkupId(true);
		form.add(treeContainer);
		
		this.renderSortTree(treeContainer);
	}

	private void renderSortName(Form form) {
		// TODO Auto-generated method stub
		sortNameField=new TextField<>("wk_sort_name",new PropertyModel<>(this.productSort,"name"));
		this.sortNameField.setEnabled(false);
		this.sortNameField.setOutputMarkupId(true);
		form.add(this.sortNameField);
		
		form.add(new AjaxButton("wk_save_sort_name") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				productSort.setParentId(sortTreeSelectedId);
				productSort.setStatus(1);
				service.addProductSort(productSort);
				
				updateMyself(target);
			}
		});
	}

	private void renderSortTree(WebMarkupContainer container) {
		// TODO Auto-generated method stub
		DefaultTreeModel treeModel=this.service.getDefaultTreeModelWithProductSort();
		
		Tree tree=new Tree("wk_tree", treeModel){
			@Override
			protected void onNodeLinkClicked(AjaxRequestTarget target,
					TreeNode node) {
				// TODO Auto-generated method stub
				super.onNodeLinkClicked(target, node);
				ProductSort sort=(ProductSort) ((DefaultMutableTreeNode) node).getUserObject();
				
				sortTreeSelectedId=sort.getId();
				
				if(validTreeSelected(target)){
					sortNameField.setEnabled(true);
					target.add(sortNameField);
				}
			}
			@Override
			protected String renderNode(TreeNode node) {
				// TODO Auto-generated method stub
				
				ProductSort sort=(ProductSort) ((DefaultMutableTreeNode) node).getUserObject();
				return sort.getName();
			}
			
		};
		container.add(tree);
	}

	private void renderDelBtn(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_del") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				if(validTreeSelected(target)){
					warnPanel.showModal(target);
				}
			}
		});
	}

	//验证tree已被选中节点
	private boolean validTreeSelected(AjaxRequestTarget target){
		if(this.sortTreeSelectedId==null){
			this.feedbackPanel.error("请选择节点");
			target.add(this.feedbackPanel);
			return false;
		}else{
			return true;
		}
	}
	
	private void updateMyself(AjaxRequestTarget target){
		JLayouterPanel layouter=(JLayouterPanel) this.getSession().getAttribute("layouter");
		
		layouter.updatePanel(JLayouterPanel.CENTER_PANEL, new ProductSortPanel(JLayouterPanel.CENTER_PANEL), target);
	}

}
